import { ConfigModule, ConfigService } from '@nestjs/config';
import { TestingModule, Test } from '@nestjs/testing';
import { getRepositoryToken, TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from 'src/modules/auth/auth.module';
import { CategoryModule } from 'src/modules/category/category.module';
import { CommonModule } from 'src/modules/common/common.module';
import { EventToPlayerModule } from 'src/modules/event-to-player/event-to-player.module';
import { EventModule } from 'src/modules/event/event.module';
import { GameRuleModule } from 'src/modules/game-rule/game-rule.module';
import { GameModule } from 'src/modules/game/game.module';
import { GroupPlayerScoreModule } from 'src/modules/group-player-score/group-player-score.module';
import { GroupModule } from 'src/modules/group/group.module';
import { GymnasiumModule } from 'src/modules/gymnasium/gymnasium.module';
import { MatchModule } from 'src/modules/match/match.module';
import { PermissionModule } from 'src/modules/permission/permission.module';
import { PlayerSignupModule } from 'src/modules/player-signup/player-signup.module';
import { PlaygroundModule } from 'src/modules/playground/playground.module';
import { RallyRuleModule } from 'src/modules/rally-rule/rally-rule.module';
import { RoleModule } from 'src/modules/role/role.module';
import { StageEventModule } from 'src/modules/stage-event/stage-event.module';
import { StageModule } from 'src/modules/stage/stage.module';
import { TournamentModule } from 'src/modules/tournament/tournament.module';
import { UserModule } from 'src/modules/user/user.module';
import { EventEmitterModule } from '@nestjs/event-emitter';

export const getTestModule = async (param?) => {
  const { providers } = param;
  const module: TestingModule = await Test.createTestingModule({
    imports: [
      EventEmitterModule.forRoot(),
      TypeOrmModule.forRootAsync({
        imports: [ConfigModule],
        inject: [ConfigService],
        useFactory: async (configService: ConfigService) => ({
          type: 'sqlite', // 数据库类型
          database: ':memory:', // 使用内存数据库
          timezone: '+08:00', // 服务器上配置的时区
          synchronize: true, // 测试时自动创建表
          autoLoadEntities: true,
          logging: false, // 测试时可关闭日志
        }),
      }),
      AuthModule,
      TournamentModule,
      CategoryModule,
      EventModule,
      StageModule,
      GroupModule,
      MatchModule,
      UserModule,
      GameModule,
      GymnasiumModule,
      PlaygroundModule,
      EventToPlayerModule,
      GroupPlayerScoreModule,
      StageEventModule,
      GameRuleModule,
      RallyRuleModule,
      PlayerSignupModule,
      RoleModule,
      PermissionModule,
      CommonModule,
    ],
    providers: providers || [],
  }).compile();

  return module;
};

// export const getTestModule = async () => {
//   const module: TestingModule = await Test.createTestingModule({
//     imports: [
//       TypeOrmModule.forFeature([
//         Category,
//         Tournament,
//         Gymnasium,
//         Playground,
//         Stage,
//         Group,
//         GroupPlayerScore,
//         MatchInfo,
//         GameInfo,
//         Event,
//         Team,
//         User,
//         EventToPlayer,
//         Stage,
//         GameRule,
//         RallyRule,
//         StageEvent,
//         PlayerSignup,
//       ]),
//       TypeOrmModule.forRootAsync({
//         imports: [ConfigModule],
//         inject: [ConfigService],
//         useFactory: async (configService: ConfigService) => ({
//           type: 'sqlite', // 数据库类型
//           database: ':memory:', // 使用内存数据库
//           timezone: '+08:00', // 服务器上配置的时区
//           synchronize: true, // 测试时自动创建表
//           autoLoadEntities: true,
//           logging: false, // 测试时可关闭日志
//         }),
//       }),
//     ],
//     // controllers: [UploadSignupController],
//     providers: [
//       TournamentService,
//       EventService,
//       TeamService,
//       UserService,
//       EventToPlayerService,
//       CategoryService,
//       StageService,
//       MatchService,
//       CreateSignupService,
//       PlayerSignupService,
//       ConfirmPlayerE2PService,
//       MatchReadyService,
//       MatchGiveUpService,
//       MatchEndService,
//       MatchScoreService,
//       GroupService,
//       GroupPlayerScoreService,
//     ],
//   }).compile();

//   return module;
// };
